﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace AJAXChatClient
{
    public static class HtmlHelpers
    {
        public static HtmlElement GetPreviousSibling(HtmlElement node)
        {
            if (node.Parent == null)
                return null;
            HtmlElementCollection children = node.Parent.Children;
            if (children.Count <= 1)
                return null;
            for (int i = 1; i < children.Count; i++)
            {
                if (children[i] == node)
                    return children[i - 1];
            }
            return null;
        }

        public static HtmlElement CloneNode(HtmlElement node, bool deep)
        {
            HtmlElement clone = node.Document.CreateElement(node.TagName);
            clone.OuterHtml = node.OuterHtml;
            if (!deep) clone.InnerHtml = "";
            return clone;
        }

        public static bool RemoveChild(HtmlElement parent, HtmlElement child)
        {
            HtmlElementCollection children = parent.Children;
            if (children.Count == 0)
                return false;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < children.Count; i++)
            {
                if (children[i] != child)
                {
                    sb.Append(children[i].OuterHtml);
                }
            }
            parent.InnerHtml = sb.ToString();
            /*string childStr = child.OuterHtml;
            string parentHtml = parent.InnerHtml;
            int index = parentHtml.IndexOf(childStr, StringComparison.OrdinalIgnoreCase);
            if (index < 0)
                return false;
            parent.InnerHtml = parentHtml.Remove(index, childStr.Length);/* */
            return true;
        }

        public static string GetStyle(HtmlElement node, string styleTag)
        {
            string styleAttr = node.Style;
            if (string.IsNullOrEmpty(styleAttr))
            {
                return null;
            }
            string[] styleParts = styleAttr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] styleArray;
            for (int i = styleParts.Length - 1; i >= 0; i--)
            {
                styleArray = styleParts[i].Trim().Split(new char[] { ':' }, 2, StringSplitOptions.None);
                if (styleArray.Length > 1 && styleArray[0].Equals(styleTag, StringComparison.OrdinalIgnoreCase))
                {
                    return styleArray[1].Trim();
                }
            }
            return null;
        }

        public static void SetStyle(HtmlElement node, string styleTag, string styleValue)
        {
            string styleStr = node.Style;
            if (string.IsNullOrEmpty(styleStr))
            {
                node.Style = styleTag + ": " + styleValue;
            }
            else
            {
                    bool flag = true;
                    string[] styleParts = styleStr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    string stylePart;
                    string[] styleArray;
                    for (int i = styleParts.Length - 1; i >= 0 && flag; i--)
                    {
                        stylePart = styleParts[i].Trim();
                        styleParts[i] = stylePart;
                        styleArray = stylePart.Split(new char[] { ':' }, 2, StringSplitOptions.None);
                        if (styleArray.Length > 1 && styleArray[0].Equals(styleTag, StringComparison.OrdinalIgnoreCase))
                        {
                            styleParts[i] = styleTag + ": " + styleValue;
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        styleStr = string.Join("; ", styleParts);
                    }
                    else
                    {
                        styleStr += "; " + styleTag + ": " + styleValue;
                    }
                    node.Style = styleStr;
            }
        }
    }
}
